package com.zc.commons.pojo;

import com.zc.commons.utils.ExceptionUtils;

public class ResponseData<T> {

    private Integer code;
    private String message;
    private T data;
    private String exception;

    public ResponseData() {
    }

    public ResponseData(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResponseData(Integer code, T data) {
        this.code = code;
        this.message = "Success!";
        this.data = data;
    }

    public ResponseData(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }


    public static <E> ResponseData<E> ok(E data) {
        return new ResponseData(200, data);
    }

    public static <E> ResponseData<E> ok(String msg, E data) {
        return new ResponseData(200, msg, data);
    }


    public static ResponseData error(String msg) {
        return error(300, msg);
    }

    public static ResponseData error(int code, String msg) {
        return new ResponseData(code, msg, null);
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getException() {
        return exception;
    }

    public void setException(Throwable e) {
        this.exception = e == null ? null : ExceptionUtils.getStackTrace(e);
    }

    @Override
    public String toString() {
        return "ResponseData{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                ", exception='" + exception + '\'' +
                '}';
    }
}
